package org.syh.boyangfighthealthclub.controller;

import org.syh.boyangfighthealthclub.entity.Book;
import org.syh.boyangfighthealthclub.entity.User;
import org.syh.boyangfighthealthclub.service.IBookService;
import org.syh.boyangfighthealthclub.service.IUserService;
import org.syh.boyangfighthealthclub.service.impl.BookServiceImpl;
import org.syh.boyangfighthealthclub.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: Ben
 * @Date: 2021/3/13 19:21
 */
@WebServlet("/admin/selectBook")
public class SelectBook extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;utf-8");
        String p=req.getParameter("page");
        IBookService iBookService=new BookServiceImpl();
        /**
         * 每页用户数
         */
        int page;
        try {
            //当前页数
            page = Integer.valueOf(p);
        } catch (NumberFormatException e) {
            page = 1;
        }
        int pageSize = 19;
        List<Book> bookList=iBookService.selectBooks(page,pageSize);

        /**
         * 总用户数
         */
        int totalUsers = iBookService.selectBook();
        //总页数
        int totalPages = totalUsers % pageSize == 0 ? totalUsers / pageSize : totalUsers / pageSize + 1;
        //本页起始用户序号
        int beginIndex = (page - 1) * pageSize;
        //本页末尾用户序号的下一个
        int endIndex = beginIndex + pageSize;
        if (endIndex > totalUsers){
            endIndex = totalUsers;
        }
        req.setAttribute("totalPages", totalPages);
        req.setAttribute("page", page);
        req.setAttribute("bookList", bookList);
        req.getRequestDispatcher("../indexBook.jsp").forward(req, resp);
    }
}
